package net.funafuna.funamail;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Message {

    private static final String FROM = "FROM";
    private static final String SUBJECT = "SUBJECT";
    private static final String TO = "TO";
    private static final String CC = "CC";
    private static final String BCC = "BCC";
    private static final String REFERENCES = "REFERENCES";

    private final Map<String, String> headers = new HashMap<String, String>();
    private String content;

    public void setFrom(String value) {
        this.headers.put(FROM, value);
    }

    public void setSubject(String value) {
        this.headers.put(SUBJECT, value);
    }

    public void setTo(String value) {
        this.headers.put(TO, value);
    }

    public void setCc(String value) {
        this.headers.put(CC, value);
    }

    public void setBcc(String value) {
        this.headers.put(BCC, value);
    }

    public void setReferences(String value) {
        this.headers.put(REFERENCES, value);
    }

    public void setOptionHeader(String key, String value) {
        if (!key.startsWith("X-")) {
            throw new IllegalArgumentException("key is not start from X-. key={" + key + "]");
        }
        this.headers.put(key, value);
    }

    public String getMailFrom() {
        String from = this.headers.get(FROM);
        if (from != null && !from.equals("")) {
            from = Util.getPlainAddress(from);
        }
        return from;
    }

    public List<String> getRcptTo() {
        final List<String> list = new ArrayList<String>();
        final String to = this.headers.get(TO);
        if (to != null && !to.equals("")) {
            list.addAll(Util.getPlainAddresses(to));
        }

        final String cc = this.headers.get(CC);
        if (cc != null && !cc.equals("")) {
            list.addAll(Util.getPlainAddresses(cc));
        }

        final String bcc = this.headers.get(BCC);
        if (bcc != null && !bcc.equals("")) {
            list.addAll(Util.getPlainAddresses(bcc));
        }
        return list;
    }

    public String getFrom() {
        String from = this.headers.get(FROM);
        if (from != null && !from.equals("")) {
            from = "From: " + Util.addressBase64Encode(from);
        }
        return from;
    }

    public String getSubject() {
        String subject = this.headers.get(SUBJECT);
        if (subject != null) {
            subject = "Subject: " + Util.base64Encode(subject);
        }
        return subject;
    }

    public String getTo() {
        String to = this.headers.get(TO);
        if (to != null && !to.equals("")) {
            to = "To: " + Util.addressBase64Encode(to);
        }
        return to;
    }

    public String getCc() {
        String cc = this.headers.get(CC);
        if (cc != null && !cc.equals("")) {
            cc = "Cc: " + Util.addressBase64Encode(cc);
        }
        return cc;
    }

    public String getReferences() {
        String references = this.headers.get(REFERENCES);
        if (references != null && !references.equals("")) {
            references = "References: " + references;
        }
        return references;
    }

    // public String getBcc() {
    // String bcc = this.headers.get(BCC);
    // if (bcc != null) {
    // bcc = "Bcc: " + bcc;
    // }
    // return bcc;
    // }

    public List<String> getOptionHeaders() {
        final List<String> opstionHeaderList = new ArrayList<String>();
        final Set<String> keys = headers.keySet();
        for (String key : keys) {
            if (!key.startsWith("X-")) {
                continue;
            }
            opstionHeaderList.add(key + ": " + this.headers.get(key));
        }
        return opstionHeaderList;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String toString() {
        // TODO
        return super.toString();
    }

}
